import User.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "Register", urlPatterns = {"/Register"})
public class Register extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            boolean flag = true;
            //中文乱码
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw=response.getWriter();


            BufferedReader br = null;
            br = new BufferedReader(new FileReader("F:\\javaEE\\IdeaProjects\\exp3\\src\\main\\user.txt"));
            // 一次读取一行
            String line = null;

            while ((line = br.readLine()) != null) {
                // 字符串的分割功能
                String[] datas = line.split(" ");
                // 判断
                if (datas[0].equals(request.getParameter("username")))
                {
                    flag=false;
                    pw.println("<script language='javascript'>alert('用户名已经存在,请重新输入')</script>");
                    pw.println("<center><button onclick=\"window.location.href='./register.jsp'\">重新注册</button></center>");
                    break;
                }}

            if(flag)
            {
                FileWriter  f=new FileWriter("F:\\javaEE\\IdeaProjects\\exp3\\src\\main\\user.txt",true);
                BufferedWriter bw=new BufferedWriter(f);
                if(request.getParameter("password").equals(request.getParameter("qrpassword")))
                {
                    String name = request.getParameter("username");
                    String email = request.getParameter("e-mail");
                    String password = request.getParameter("password");
                    String qrpassword = request.getParameter("qrpassword");
                    String phone = request.getParameter("phone");
                    String info=name+" "+email+" "+password+" "+phone+" "+"woman-1254453_640.jpg";

                    //在文件中写入数据
                    bw.write(info);
                    bw.newLine();
                    bw.flush();
                    //关闭文件流
                    bw.close();
                   // pw.println("<center><button onclick=\"window.location.href='./show.jsp'\">注册成功,到用户中心</button></center>");
                    MessageDTO messageDTO=new MessageDTO(name,email,password,phone,"woman-1254453_640.jpg");
                    request.getSession().setAttribute("LocalUser",messageDTO);
                    response.sendRedirect("/show.jsp");
                }
                else
                {
                    pw.println("两次密码不一致,请重新注册");
                    pw.println("<center><button onclick=\"window.location.href='./register.jsp'\">返回注册</button></center>");
                }


            }

        }

        finally {
            out.close();
        }
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
